/*
 * Copyright (c) 2005 -2008 Reliable Source, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Mozilla Public License v.1.1,
 * BSD, and Eclipse Public License v1.0 which accompanies this
 * distribution and is available at:
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 * 		http://www.opensource.org/licenses/bsd-license.php
 * 		http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * Contributors:
 *     IBM Corporation	     -  http://ibm.com
 *     	- initial API and implementation
 *
 *     Reliable Source, Inc. - http://reliablesource.org
 *
 */
package org.reliablesource.client.ui.viewers;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.jface.viewers.ContentViewer;
import org.eclipse.jface.viewers.IOpenListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.reliablesource.model.core.IDisposable;

/**
 * 
 * ReliableViewer.java<br>
 * Created on: Apr 19, 2007 <br>
 *
 * @author <a href="mailto:dennis.park@gmail.com">Dennis Park</a>
 *
 * @category
 * @since 1.0
 */
abstract public class ReliableViewer extends ContentViewer implements
	IReliableViewer {

    protected IOpenListener openListener;
    protected ContentViewer contentViewer;
    protected List<ContentViewer> children;
    protected Composite contentComposite;
    protected StructuredSelection currentSelection = StructuredSelection.EMPTY;

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.Viewer#getControl()
     */
    @Override
    public Control getControl() {
	return this.contentComposite;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.Viewer#getSelection()
     */
    @Override
    public ISelection getSelection() {
	return this.currentSelection;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.Viewer#refresh()
     */
    @Override
    public void refresh() {
	this.contentViewer.refresh();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.Viewer#setSelection(org.eclipse.jface.viewers.ISelection)
     */
    @Override
    public void setSelection(ISelection _selection) {
	setSelection(_selection, false);
    }

    public void init() {
	this.children = new LinkedList<ContentViewer>();
    }

    abstract public void createViewerControl(Composite _parent);

    /**
     * @return the children
     */
    public Viewer[] getChildren() {
	return this.children.toArray(new Viewer[this.children.size()]);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.reliablesource.client.ui.viewers.IReliableViewer#addChild(org.eclipse.jface.viewers.ContentViewer)
     */
    public boolean addChild(ContentViewer _child) {
	if (null != _child)
	    if (!this.children.contains(_child))
		return this.children.add(_child);
	return false;
    }
    
    /*
     * (non-Javadoc)
     * @see org.reliablesource.client.ui.viewers.IReliableViewer#removeChild(org.eclipse.jface.viewers.ContentViewer)
     */
    public boolean removeChild(ContentViewer _child) {
	if (null != _child)
	    if (this.children.contains(_child))
		return this.children.remove(_child);
	return false;
    }
    

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.Viewer#setSelection(org.eclipse.jface.viewers.ISelection,
     *      boolean)
     */
    @Override
    public void setSelection(ISelection _selection, boolean _reveal) {
	if (null == _selection)
	    _selection = StructuredSelection.EMPTY;

	if (_selection.equals(getSelection()))
	    return;

	this.currentSelection = (StructuredSelection) _selection;
	this.contentViewer.setSelection(this.currentSelection, _reveal);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.services.IDisposable#dispose()
     */
    public void dispose() {
	Iterator<ContentViewer> _it = this.children.iterator();
	while (_it.hasNext()) {
	    ContentViewer _child = _it.next();
	    if (_child instanceof IDisposable)
		((IDisposable) _child).dispose();
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.reliablesource.client.ui.viewers.IReliableViewer#getContentViewer()
     */
    public Viewer getContentViewer() {
	return this.contentViewer;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.StructuredViewer#addOpenListener(org.eclipse.jface.viewers.IOpenListener)
     */
    public void addOpenListener(final IOpenListener _listener) {
	this.openListener = _listener;

	Viewer[] _children = getChildren();
	for (int i = 0; i < _children.length; i++)
	    if (_children[i] instanceof StructuredViewer)
		((StructuredViewer) _children[i]).addOpenListener(_listener);
	    else if (_children[i] instanceof ReliableViewer)
		((ReliableViewer) _children[i]).addOpenListener(_listener);
    }
    
    /*
     * (non-Javadoc)
     * @see org.reliablesource.client.ui.viewers.IReliableViewer#setFocus()
     */
    public boolean setFocus() {
	return getControl().setFocus();
    }

}
